#include <bits/stdc++.h>
using namespace std;
void err() {
	cout << "ERR" << endl;
	char ch;
	while ((ch = getchar()) != '\n');
}
struct Addr {
	int a, b, c, d, e;
};
int hash_addr(Addr asdf) {
	return asdf.a * 2 + asdf.b * 3 + asdf.c * 5 + asdf.d * 7 + asdf.e * 11;
}
map<int, int> serv;
int read(char nxt) {
	int n; char ch; bool flag=false;
	while (!isdigit(ch = getchar()));
	n = ch - '0'; if (!n) flag = true;
	while (isdigit(ch = getchar())) {
		n = n * 10 + ch - '0';
		if (n >= 100000) return 99999;
	}
	if (ch != nxt) {
    	return 99999;
	}
	if (n && flag) return 99999;
	return n;
}
int read2() {
	int n; char ch;
	while (!isdigit(ch = getchar()));
	n = ch - '0'; if (!n) return 99999;
	while (isdigit(ch = getchar())) {
		n = n * 10 + ch - '0';
		if (n >= 100000) return 99999;
	}
	return n;
}
int main() {
    freopen("network.in", "r", stdin);
    freopen("network.out", "w", stdout);
    int n, cnt=1; char ch; Addr t;
    cin >> n; getchar();
    for (int i=1; i<=n; i++) {
    	ch = getchar();
    	getchar(); getchar(); getchar(); getchar(); getchar(); getchar();
    	t.a = read('.');
    	if (t.a > 255) {
    		err(); continue;
		}
		
    	t.b = read('.');
    	if (t.b > 255) {
    		err(); continue;
		}
		
		t.c = read('.');
    	if (t.c > 255) {
    		err(); continue;
		}
		
		t.d = read(':');
    	if (t.d > 255) {
    		err(); continue;
		}
		
		t.e = read2();
    	if (t.e > 65535) {
    		err(); continue;
		}
		//cout << t.a << t.b << t.c << t.d << t.e << endl;
		if (ch == 'S') {
			if (serv.count(hash_addr(t))) {
				cout << "FAIL" << endl;
			} else {
				serv[hash_addr(t)] = cnt;
				cout << "OK" << endl;
			}
		} else {
			if (serv.count(hash_addr(t))) {
				cout << serv[hash_addr(t)] << endl;
			} else {
				cout << "FAIL" << endl;
			}
		}
		cnt++;
	}
    return 0;
}
